/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.DataRetrieval;

import Utilities.Constants;
import Utilities.Constants.LogsRetrievalLimit;
import Utilities.PermissionsConstants;

/**
 * Instances of this class are used by the user interface when a request to
 * retrieve logs data is sent to the data retrieval service.
 *
 * @author Angel Sanadinov
 */
public class LogsDataRetrievalRequest extends LocalResourceRequest
{
    private LogsRetrievalLimit limit;   //the logs retrieval limit
    private int limitingResourceId;     //the id of limiting resource (user/server)
    private String severity;            //the limiting severity (when limiting by severity)

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     *
     * @see LogsRetrievalLimit#INVALID_LIMIT
     * @see Constants#INVALID_NUMERIC_RESOURCE_ID
     */
    public LogsDataRetrievalRequest()
    {
        super();
        setRequestedAction(PermissionsConstants.ACTION_RETRIEVE_LOGS);
        this.limit = LogsRetrievalLimit.INVALID_LIMIT;
        this.limitingResourceId = Constants.INVALID_NUMERIC_RESOURCE_ID;
        this.severity = null;
    }

    /**
     *  Constructs a request for the retrieval of all available logs.
     *
     * @param requestorId the id of the user that made the request
     */
    public LogsDataRetrievalRequest(int requestorId)
    {
        super(requestorId);
        setRequestedAction(PermissionsConstants.ACTION_RETRIEVE_LOGS);
        this.limit = LogsRetrievalLimit.NO_LIMIT;
        this.limitingResourceId = Constants.INVALID_NUMERIC_RESOURCE_ID;
        this.severity = null;
    }

    /**
     * Constructs a request for the retrieval of all logs, limited by the
     * supplied resource id.
     *
     * @param requestorId the id of the user that made the request
     * @param limitingResourceId the id of the limiting resource
     * @param limit the type of limit that is to be used
     */
    public LogsDataRetrievalRequest(int requestorId, int limitingResourceId, LogsRetrievalLimit limit)
    {
        this(requestorId);
        this.limitingResourceId = limitingResourceId;
        this.limit = limit;
    }

    /**
     * Constructs a request for the retrieval of all logs, limited by the specified
     * severity.
     *
     * @param requestorId the id of the user that made the request
     * @param severity the severity, used to limit the logs retrieval
     */
    public LogsDataRetrievalRequest(int requestorId, String severity)
    {
        this(requestorId);
        this.limit = LogsRetrievalLimit.SEVERITY;
        this.severity = severity;
    }

    /**
     * Retrieves the logs retrieval limit associated with this object.
     *
     * @return the logs retrieval limit, if available or <code>INVALID_LIMIT</code>
     *         otherwise
     */
    public LogsRetrievalLimit getLimit()
    {
        return limit;
    }

    /**
     * Retrieves the id of the resource that is used for limiting the logs retrieval.
     *
     * @return the limiting resource id, if available or <code>INVALID_NUMERIC_RESOURCE_ID</code>
     *
     * @see Constants#INVALID_NUMERIC_RESOURCE_ID
     */
    public int getLimitingResourceId()
    {
        return limitingResourceId;
    }

    /**
     * Retrieves the severity that is used for limiting the logs retrieval.
     *
     * @return the limiting severity, if available or <code>null</code> otherwise
     */
    public String getSeverity()
    {
        return severity;
    }

    /**
     * Sets the logs retrieval limit.
     *
     * @param limit the retrieval limit
     */
    public void setLimit(LogsRetrievalLimit limit)
    {
        this.limit = limit;
    }

    /**
     * Sets the resource id to be used when limiting logs retrieval.
     *
     * @param limitingResourceId the limiting resource id
     */
    public void setLimitingResourceId(int limitingResourceId)
    {
        this.limitingResourceId = limitingResourceId;
    }

    /**
     * Sets the severity that is to be used when limiting logs retrieval.
     *
     * @param severity the limiting severity
     */
    public void setSeverity(String severity)
    {
        this.severity = severity;
    }

    /**
     * Checks if the resource id associated with the request is valid. <br><br>
     *
     * <b>Note:</b> <i>Always returns <code>false</code> for logs, because
     * request for single log data are not allowed.</i>
     *
     * @return <code>false</code>
     */
    @Override
    protected boolean isResourceIdValid()
    {
        return false;
    }

    /**
     * Checks if the supplied request data is valid.
     *
     * @return <code>true</code> if the data is valid or <code>false</code> otherwise
     */
    private boolean isRequestDataValid()
    {
        if(limit != null && limit != LogsRetrievalLimit.INVALID_LIMIT
                && (limit == LogsRetrievalLimit.NO_LIMIT
                    || (limit == LogsRetrievalLimit.SEVERITY && severity != null && severity.length() == 1)
                    || (limitingResourceId > Constants.INVALID_NUMERIC_RESOURCE_ID)))
            return true;
        else
            return false;
    }

    @Override
    public boolean isValid()
    {
        if(super.isValid() && isRequestDataValid())
            return true;
        else
            return false;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (limit,limitingResourceId,severity)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return LogsDataRetrievalRequest.class.getSimpleName()
                + ": (" + limit + "," + limitingResourceId + "," + severity + ")";
    }
}
